package com.abocode.aichat.utils;

import java.io.*;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.file.FileSystems;
import java.util.Properties;

/**
 * 
 * @author  guanxf
 *
 */
public class SystemUtils {
	private static String  properiesName = "im-server.properties";

	public SystemUtils() {

	}
	public static String getProperty(String key) {
		String value = "";
		InputStream is = null;
		try {
			is = SystemUtils.class.getClassLoader().getResourceAsStream(
					properiesName);
			Properties p = new Properties();
			p.load(is);
			value = p.getProperty(key);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				is.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return value;
	}

	public Properties getProperties() {
		Properties p = new Properties();
		InputStream is = null;
		try {
			is = SystemUtils.class.getClassLoader().getResourceAsStream(
					properiesName);
			p.load(is);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				is.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return p;
	}

	public void writeProperty(String key, String value) {
		InputStream is = null;
		OutputStream os = null;
		Properties p = new Properties();
		try {
			is = new FileInputStream(properiesName);
			p.load(is);
			os = new FileOutputStream(SystemUtils.class.getClassLoader().getResource(properiesName).getFile());

			p.setProperty(key, value);
			p.store(os, key);
			os.flush();
			os.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (null != is)
					is.close();
				if (null != os)
					os.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

	/**
	 * 获取服务器相关信息
	 * @return
	 */
  	public static  InetAddress getAddress() {
		InetAddress domain=null;
		try { domain= InetAddress.getLocalHost();}
		catch (UnknownHostException e) {}
		return domain;
	}

	public static String getSecurityPath() {
		String path="";
		try {
			path =SystemUtils.class.getClassLoader().getResource("").getPath()+"security"+File.separator;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return  path;
	}

	public static void main(String[] args) {
	 //System.out.println(SystemUtils.getProperty("fileSourceUri"));
       System.out.println(SystemUtils.getSecurityPath());
	}

}
